package com.maple.common.utils

import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.ScaleXSpan
import java.util.UUID

/**
 * Tip:
 * Create by SeVen on 2023/9/9 17:22
 */
object StringUtils {

    @JvmStatic
    fun splitStringEvery(input: String, count: Int): MutableList<String> {
        val result = mutableListOf<String>()
        var index = 0
        while (index < input.length) {
            val chunkSize = kotlin.math.min(count, input.length - index)
            result.add(input.substring(index, index + chunkSize))
            index += chunkSize
        }
        return result
    }

    /**
     * 获取UUID去除中间的横线
     */
    @JvmStatic
    fun getUUID(): String {
        val uuid = UUID.randomUUID().toString()
        return uuid.replace("-".toRegex(), "")
    }

    /**
     * @param str 待对齐字符串
     * @param size 对齐字符串的长度
     */
    @JvmStatic
    fun justifyString(str: String, size: Int): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder()
        if (TextUtils.isEmpty(str)) {
            return spannableStringBuilder
        }
        val chars = str.toCharArray()
        if (chars.size >= size || chars.size == 1) {
            return spannableStringBuilder.append(str)
        }
        val length = chars.size
        val scale: Float = (size - length) / (length - 1).toFloat()
        for (i in 0 until length) {
            spannableStringBuilder.append(chars[i])
            if (i != length - 1) {
                val span = SpannableString("　")//全
                span.setSpan(ScaleXSpan(scale), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                spannableStringBuilder.append(span)
            }
        }
        return spannableStringBuilder
    }

    @JvmStatic
    fun isEmpty(value: String?): Boolean {
        return TextUtils.isEmpty(value) || "null" == value || "NULL" == value
    }
}